const pageSize = 5; //规定每页请求5条数据
let currentPage = 1; //当前页数
const topCateForm = $("#topCateForm");
getTopCate(currentPage);
const fields = {
  //校验用户名，对应name表单的name属性
  categoryName: {
    validators: {
      //不能为空
      notEmpty: {
        message: "分类名不能为空"
      }
    }
  }
};
//重置表单
function resetForm() {
  $(".modal-cate1Add").modal("hide");
}
//表单验证
const valid = new Vaild(topCateForm, fields, addTopCate, resetForm);

function setPage(totalPages) {
  $("#paginator").bootstrapPaginator({
    // 指定bootstrap版本
    bootstrapMajorVersion: 3,
    // 当前页
    currentPage: currentPage,
    // 总页数
    totalPages: totalPages,

    // 当页面被点击时触发
    onPageClicked: function (a, b, c, page) {
      currentPage = page;
      getTopCate(currentPage);
    }
  });
}
function getTopCate(currentPage) {
  $.ajax({
    url: "/category/queryTopCategoryPaging",
    data: {
      page: currentPage,
      pageSize: pageSize
    },
    dataType: "json",
    success: function (res) {
      $(".common-table tbody").html(template("tmp", res));
      currentPage = res.page;
      let totalPages = Math.ceil(res.total / res.size);
      setPage(totalPages);
    }
  });
}
//添加一级分类按钮点击事件
// $("#btn-add").on("click", addTopCate);
//添加一级分类
function addTopCate() {
  $.ajax({
    url: "/category/addTopCategory",
    type: "post",
    data: topCateForm.serialize(),
    dataType: "json",
    success: function (res) {
      $(".modal-cate1Add").modal("hide");
      topCateForm[0].reset();
      if (res.success) {
        getTopCate(currentPage);
      } else {
        alert("新增分类失败,请联系管理员");
      }
    }
  });
}
